import java.awt.Color;
import java.awt.Graphics;
import java.awt.Point;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;

import javax.imageio.ImageIO;
import javax.swing.BoxLayout;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.Timer;
/**
 * The sailing state. 
 * All the activities performed during sailing 
 * are defined here
 * @author Hobosapiens
 */
public class Sailing extends State
{
    JPanel main;
    int tickTime, runTime, ticksLeft, duration, numPoints, currentTick;
    Timer foodTimer, finishTimer;
    BadThings badThings;
    Point[] shipMove;

    /**
     * Constructor
     * @param s
     */
    public Sailing(Ship s)
    {
        super();
        theShip = s;
        badThings = new BadThings(theShip);
        
        tickTime = 1;
        runTime = 6;
        ticksLeft = runTime/tickTime;
        
        foodTimer = new Timer(tickTime*1000, tickListener);
        finishTimer = new Timer(runTime*1000, runListener);
        
        numPoints = 50;
        int distTravel = 30;
        shipMove = new Point[numPoints];
        for (int i=0; i < numPoints; i++){
        	shipMove[i] = new Point(distTravel*i, 270);
        }
        
        
    }
   /**
    * start the timer
    * @param duration  
    */
    public void start(int duration){
      	 runTime = duration;
      	 ticksLeft = runTime/tickTime;
         finishTimer.setDelay(duration*1000);
         
    	 foodTimer.restart();
    	 foodTimer.start();
    	 finishTimer.restart();
    	 finishTimer.start();
    	 
    	 //shouldn't the food timer also be dependent on the pace ( the "days" are passing faster so you still need to 
    	 // eat the same amount of times in one sailing state
    	 
    	// System.out.println("Sailing state starting with duration: " + runTime);
    }
    
    /**
     * Stops the timers
     */
    public void stop(){
    	foodTimer.stop();
		finishTimer.stop();
    }
    
    /**
     * Periodic timer for ship attribute adjustment
     */
    private ActionListener tickListener = new ActionListener() {
    	int foodTick, waterTick, moneyTick;
        public void actionPerformed(ActionEvent evt) {
  
        	if (!theShip.getInventory().consumeFood(theShip.getRations())){
        		status.firePropertyChange("die-hunger", false, true);
        		System.out.println("You have died due to hunger");
        		stop();
        	}
        	if (!theShip.getInventory().consumeWater(theShip.getRations())){
        		status.firePropertyChange("die-thurst", false, true);
        		System.out.println("You have died due to thirst");
        		stop();
        	}
        	
        	if (Math.random() > .8){
        		if (!badThings.randomEvent()){
        			foodTimer.stop();
                	finishTimer.stop();
        			status.firePropertyChange("storm", false, true);
        		}
        	}
        	
        	theShip.getInventory().addMoney(15);
        	ticksLeft--;
        	repaint();
        	status.firePropertyChange("inventory", false, true);
        }
    };
    
    private ActionListener runListener = new ActionListener() {
        public void actionPerformed(ActionEvent evt) {
        	foodTimer.stop();
        	finishTimer.stop();
        	//System.out.println("Change state");
        	
        	status.firePropertyChange("finished Sailing", false, true);
        }
    };
    
    /**
     * Painting the ocean
     */
    public void paintComponent(Graphics g)
    {
    	super.paintComponent(g);
        try
        {
            g.drawImage(ImageIO.read(new File("ocean.jpg")), 0, 0, null);
            if (shipMove[currentTick].x > 650)
            	currentTick = 0 ;
            //System.out.println(shipMove[currentTick].x + "," + shipMove[currentTick].y);
            g.drawImage(ImageIO.read(new File("ship.png")), shipMove[currentTick].x, shipMove[currentTick].y, null);
            currentTick++;
        }
        catch (IOException e)       {e.printStackTrace();}
        
       // g.setColor(Color.yellow);
        //g.fillRect(100, 100, ticksLeft*80, 200);
       // System.out.println("drawing rect, ticksLeft: "+ticksLeft);
    }
    
    /**
     * Type
     */
    public String getType(){
    	return "Sailing";
    }

}